// Filename:    nhod.c
// Author:      John Van Drasek
// Date:        16 December 2006
// Description:
//


// Includes
// LUA
extern"C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
// User defined includes
#include "AutoJob_Server.h"
#include "nhodLogin.h"
#include "nhodChat.h"

// nhodHelp
//
// Description: Display the help menu for nhod
//
//  Input: NA
//  Output: NA
void AutoJobHelp()
{
	printf("\n");
	printf("*****************************************************************\n");
	printf("*** help: ****\n");
	printf("**************\n");
	printf("help              - Display list of commands\n");
	printf("exit              - Shutdown all threads and exit\n");
	printf("usersOnline       - Lists all the users online\n");
	printf("*****************************************************************\n\n");
}


// nhodExec
//
//  Description: Call the function specified by the command line input
//
//  Input: char* str - A string entered on nhod prompt to indicate which
//          function is to be called.
//  Output: NA
void AutoJobExec(char* str)
{

	if(strcmp(str,"help") == 0)
	{
		AutoJobHelp();
	}
	if(strcmp(str,"usersOnline") == 0)
	{
		// Print the logged in users list.
		PrintUsersOnline(loggedInUsersList);
	}
}

void PrintMessage()
{
	printf("\n\n\nType \"exit\" and press enter to close this window\n");
}

//// Declare any and all libs used in this code as well as the script file
//static const luaL_reg LuaLibs[] =
//{
//	{ "base", luaopen_base },
//	{ NULL, NULL }
//};
//
//// Open all the libs declared above
//static void OpenLibs(lua_State* lua_state)
//{
//	const luaL_reg* lua_lib;
//
//	for (lua_lib = LuaLibs; lua_lib->func != NULL; lua_lib++)
//	{
//		lua_lib->func(lua_state);
//		lua_settop(lua_state, 0);
//	}
//}

// main
//
//  Description: The main nhod entry point
//
//  Input: NA
//  Output: int - Returns 0 when finished
int main()
{
	//char* scriptName = NULL;

	//if(argc != 2)
	//{
	//	printf("Please provide a script to execute as the input to the program.\n");
	//	return 0;
	//}
	//else
	//{
	//	int size = strnlen(argv[1], MAXSIZE);
	//	scriptName = (char*)malloc(size+1);
	//	memset(scriptName, 0, size+1);
	//	memcpy(scriptName, argv[1], size+1);
	//}

	//// Declare the lua state
	//lua_State *lua_state;
	//// Open the lua state
	//lua_state = lua_open();
	//// Load the libs
	//OpenLibs(lua_state);

	//// Run the lua script
	//luaL_dofile(lua_state, scriptName);

	//// Destroy the lua state
	//lua_close(lua_state);

	char str[COMMANDSIZE];
	memset(str,0,COMMANDSIZE);

	startLoginServer();

	printf("\n******************\n");
	printf("* AutoJob Server *\n");
	printf("******************\n\n");
	printf("type: \"help\" for Help menu.\n\n\n");

	while((strcmp(((char*)(&str)),"exit") != 0))
	{
		memset(str,0,COMMANDSIZE);
		scanf("%s",str);  // FIXME: this needs to be changed, but scanf_s causes problems :/ need to look into it
		AutoJobExec(((char*)(&str)));
	}

	stopLoginServer();

	return 0;
}